/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package library.Configuration;

import java.io.FileInputStream;

import org.apache.log4j.*;

/**
 * A configuration file can be referenced as an URL (applet mode) or as a file
 * reference (application mode)
 * 
 * This class embeds all specificities related to file reference-based 
 * configuration file
 * 
 * @author JNC
 */
public class ConfigFromFile extends Config {
    /**
     * Name of the configuration file
     * The configuration file is opened with the first call to read a config value
     * As this time is not predictable, the name of the configuration file
     * is recorded as soon as it is known
     */
    private static String _configFilename = null;
    /**
     * The log4j logger
     */
    private static final Logger _logger = Logger.getLogger(ConfigFromFile.class);    
    
    /**
     * Sets the filename that references the config file
     * @param myFilename
     * The filename that represents the config file
     */
    protected void setConfigFilename(String myFilename) {
        _logger.debug("Config file is accessed via file " + myFilename);
       _configFilename = myFilename; 
    }

    /**
     * Opens the filename to get the config file
     * @return
     * A reference to the config file
     */
    protected FileInputStream openConfigFile() {    
        if (_configFilename != null) {
            try {
                _logger.info("Config filename = " + _configFilename);
                return new FileInputStream(_configFilename);
            }
            catch(java.io.FileNotFoundException exception) {
                throw new FatalConfigurationException("FileNotFoundException caught while opening file " + _configFilename + " (" + exception + ")");
            }
        } else {
            throw new FatalConfigurationException("Attempt to read a configuration value while filename is null");
        }
    }
}
